/* ----------------------------------------------------------------------------------------------------------------
 *  RendererCarrier
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.entity 
{
	import flash.geom.Rectangle;
	import pure.engine.core.VisibleCore;
	import pure.engine.renderer.AnimeRenderer;
	import pure.engine.renderer.IAnime;
	import pure.engine.renderer.IRenderer;
	import pure.engine.renderer.IShape;
	import pure.engine.renderer.IText;
	import pure.engine.renderer.Renderer;
	import pure.engine.renderer.ShapeRenderer;
	import pure.engine.renderer.TextRenderer;
	import pure.engine.core.ns_despair;
	use namespace ns_despair;
	
	/**
	 * 渲染器携带者
	 * [Author] - PURE
	 * 
	 */
public class RendererCarrier extends VisibleCore
{
	
	

	/**
	 * 首渲染器
	 */
	public function get headRenderer():IRenderer 
	{ 
		return m_rendererList[0]; 
	}
	
	
	/**
	 * 尾渲染器
	 */
	public function get tailRenderer():IRenderer
	{
		return m_rendererList[m_numRenderer - 1];
	}


	/**
	 * 生成动画渲染器
	 * 
	 * [Param] - section  片段名字
	 * [Param] - name     渲染器名字
	 */
	public function createAnimeRenderer(section:String, name:String = null):IAnime
	{
		var R:AnimeRenderer = new AnimeRenderer();
		
		R.setSection(section);
		_addRenderer(R, name);
		return R;
	}
	
	
	/**
	 * 生成图形渲染器
	 * 
	 * [Param] offsetX
	 * [Param] offsetY
	 * [Param] name
	 */
	public function createShapeRenderer(offsetX:Number = 0, offsetY:Number = 0, name:String = null):IShape
	{
		var R:ShapeRenderer = new ShapeRenderer(offsetX, offsetY);
		
		_addRenderer(R, name);
		return R;
	}
	
	
	/**
	 * 生成文本渲染器
	 * 
	 * [Param] text
	 * [Param] offsetX
	 * [Param] offsetY
	 * [Param] gapX
	 * [Param] gapY
	 * [Param] name
	 */
	public function createTextRenderer(text:String, offsetX:Number = 0, offsetY:Number = 0, gapX:Number = 0, gapY:Number = 0, name:String = null):IText
	{
		var R:TextRenderer = new TextRenderer(text, offsetX, offsetY, gapX, gapY);
		
		_addRenderer(R, name);
		return R;
	}
	
	
	/**
	 * 获取渲染器(名字)
	 * 
	 * [Param] name
	 */
	public function getRenderer(name:String):IRenderer 
	{
		var i:int;
		
		for (i = 0; i < m_numRenderer; i++) 
		{
			if (m_rendererList[i].m_name == name) 
			{
				return m_rendererList[i];
			}
		}
		return null;
	}
	
	
	
	

	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */

	
	
	// 世界区域
	public function get worldRect():Rectangle
	{
		_worldRect.x = m_x - m_range;
		_worldRect.y = m_y - m_range;
		return _worldRect; 
	}

	
	/**
	 * 加入渲染器
	 * 
	 * [Param] - R
	 * [Param] - name
	 */
	ns_despair function _addRenderer(R:Renderer, name:String):void
	{
		R.m_name = name;
		m_rendererList[m_numRenderer++] = R;
		m_fragmentDirtyList[m_numRenderer] =  m_fragmentDirtyList[m_numRenderer - 1] = 0;
	}
	
	
	/**
	 * 更新渲染器
	 * 
	 * [Feature] - [-1]不可见， [0]画像不变，[1]画像变化，[2]范围变化
	 */
	ns_despair function updateRenderer(deltaTime:int):void
	{
		var R:Renderer;
		var i:int, flag:int;
		var newRange:Number;
		
		// 碎片状态还原
		m_fragmentDirtyList[m_numRenderer] = 0;
		for (i = 0; i < m_numRenderer; i++) 
		{
			R     =  m_rendererList[i];
			flag  =  R.m_dirtyFlag;
			
			// 变为不可见
			if(flag == -1)
			{
				m_fragmentDirtyList[i] = flag;
				m_fragmentDirtyList[m_numRenderer] = i + 1;
				R.m_dirtyFlag = 0;
				return;
			}
			
			// 更新渲染器
			else if(R.m_visible)
			{	
				R.advance(deltaTime);
			}
			
			// 未变化
			if(flag == 0)  continue;
			
			// 碎片变化
			m_fragmentDirtyList[i] = flag;
			m_fragmentDirtyList[m_numRenderer] = i + 1;
			R.m_dirtyFlag = 0;
			
			if (flag == 2)  
			{
				m_rangeDirty = true;
			}
		}
		
		// 面积变化
		if(m_rangeDirty)
		{
			cachedArray.length = 0;
			for (i = 0; i < m_numRenderer; i++)
			{
				R = m_rendererList[i];
				if(R.m_visible)
				{
					cachedArray.push(R.m_oldRange);
				}
			}
			cachedArray.sort(Array.NUMERIC | Array.DESCENDING);
			
			newRange = cachedArray[0];
			if (newRange != m_range)
			{
				m_range            =  newRange;
				_worldRect.width   =  newRange * 2;
				_worldRect.height  =  newRange * 2;
			}
			m_rangeDirty = false;
		}
	}
	
	
	 // 毁灭
	override ns_despair function destroy():void
	{
		super.destroy();
		
		while (--m_numRenderer > -1)
		{
			m_rendererList[m_numRenderer].destroy();
		}
		m_rendererList.length  =  m_numRenderer  =  0;
	}
	
	
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair var m_rendererList:Vector.<Renderer> = new Vector.<Renderer>();  // 渲染器列表

	ns_despair var m_fragmentDirtyList:Vector.<uint> = new Vector.<uint>();  // 碎片变化列表，尾项0表示未变化.

	ns_despair var m_numRenderer:int;  // 渲染器数目
	
	ns_despair var m_rangeDirty:Boolean;  // 面积变化
	
	
	
	
	//======================
	// Cached Object
	//======================
	
	
	ns_despair static var cachedArray:Array = [];
	
	ns_despair var _worldRect:Rectangle = new Rectangle();

	
}

}